package com.djmw.mouse.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.SearchFilter;

import com.djmw.mouse.entity.JobSearchResult;
import com.djmw.mouse.solr.JobSearchDaoSolr;

@Component
public class JobSearchService {
    @Autowired
    JobSearchDaoSolr jobSearchDaoSolr;

    public JobSearchResult searchPosition(Map<String, Object> searchParams, int pageNumber, int pageSize,
            String sortType, String sortDir) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Direction.DESC, "refresh_date");
        } else {
            sort = new Sort("d".equalsIgnoreCase(sortDir) ? Direction.DESC : Direction.ASC, sortType);
        }
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        return jobSearchDaoSolr.search(filters, pageRequest);
    }
}
